package cn.iocoder.yudao.module.member.controller.admin.confrebate;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.member.controller.admin.confrebate.vo.MemberConfRebatePageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.confrebate.vo.MemberConfRebateRespVO;
import cn.iocoder.yudao.module.member.controller.admin.confrebate.vo.MemberConfRebateSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.memberconf.confrebate.MemberConfRebateDO;
import cn.iocoder.yudao.module.member.service.memberconf.confrebate.MemberConfRebateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 返佣政策配置")
@RestController
@RequestMapping("/member/conf-rebate")
@Validated
public class MemberConfRebateController {

    @Resource
    private MemberConfRebateService confRebateService;

    @PostMapping("/create")
    @Operation(summary = "创建返佣政策配置")
    @PreAuthorize("@ss.hasPermission('member:conf-rebate:create')")
    public CommonResult<Long> createConfRebate(@Valid @RequestBody MemberConfRebateSaveReqVO createReqVO) {
        return success(confRebateService.createConfRebate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新返佣政策配置")
    @PreAuthorize("@ss.hasPermission('member:conf-rebate:update')")
    public CommonResult<Boolean> updateConfRebate(@Valid @RequestBody MemberConfRebateSaveReqVO updateReqVO) {
        confRebateService.updateConfRebate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除返佣政策配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:conf-rebate:delete')")
    public CommonResult<Boolean> deleteConfRebate(@RequestParam("id") Long id) {
        confRebateService.deleteConfRebate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得返佣政策配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:conf-rebate:query')")
    public CommonResult<MemberConfRebateRespVO> getConfRebate(@RequestParam("id") Long id) {
        MemberConfRebateDO confRebate = confRebateService.getConfRebate(id);
        return success(BeanUtils.toBean(confRebate, MemberConfRebateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得返佣政策配置分页")
    @PreAuthorize("@ss.hasPermission('member:conf-rebate:query')")
    public CommonResult<PageResult<MemberConfRebateRespVO>> getConfRebatePage(@Valid MemberConfRebatePageReqVO pageReqVO) {
        PageResult<MemberConfRebateDO> pageResult = confRebateService.getConfRebatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MemberConfRebateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出返佣政策配置 Excel")
    @PreAuthorize("@ss.hasPermission('member:conf-rebate:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConfRebateExcel(@Valid MemberConfRebatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MemberConfRebateDO> list = confRebateService.getConfRebatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "返佣政策配置.xls", "数据", MemberConfRebateRespVO.class,
                        BeanUtils.toBean(list, MemberConfRebateRespVO.class));
    }

}